package com.yidu.qianfeng.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.qianfeng.common.bean.finance.Finance;
import com.yidu.qianfeng.common.bean.finance.FinanceCondition;
import com.yidu.qianfeng.common.bean.finance.Income;
import com.yidu.qianfeng.common.bean.finance.Spend;
import com.yidu.qianfeng.common.bean.waybill.WaybillCondition;
import com.yidu.qianfeng.common.util.LayuiJsonWrap;
import com.yidu.qianfeng.service.IncomeServiceInter;
import com.yidu.qianfeng.service.SpendServiceInter;
import com.yidu.qianfeng.service.impl.FinanceServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: 戴超
 * @Description: 收入明细
 */
@Controller
public class SpendController {
    @Resource(name = "spendServiceImpl")
    private SpendServiceInter spendService;

    @Resource(name = "financeServiceImpl")
    private FinanceServiceImpl financeService;

    @RequestMapping("/spend")
    public String income(){
        return "finance/spend";
    }
    /**
     * 分页查询开支
     * @param
     * @return
     */
    @RequestMapping("/selectAllSpend")
    @ResponseBody
    public Map selectAllSpend(@Param("page")int page, @Param("limit")int limit){
        List<Spend> spend = spendService.selectAllSpend((page-1)*limit,limit);
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",spendService.selectSpendCount());
        map.put("data",spend);
        return map;
    }
    /**
     * 根据条件查询开支明细
     * @return
     */
    @RequestMapping("/selectSpendByCondition")
    @ResponseBody
    public LayuiJsonWrap selectSpendByCondition(@RequestParam("financeCondition")String financeCondition){

        FinanceCondition condition = null;
        try {
            condition = new ObjectMapper().readValue(financeCondition, FinanceCondition.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(condition.getFinanceWay());
        return spendService.selectSpendByCondition(condition);
    }
    /**
     * 新增开支
     * @param spend
     * @return
     */
    @PostMapping("/addSpend")
    @ResponseBody
    public String addIncome(@RequestBody Spend spend){
        UUID uuid = UUID.randomUUID();
        String str = uuid.toString();
        spend.setSpendId(str);
        Finance finance = new Finance();
        finance.setInspendId(str);
        //资金项目
        finance.setInspendFound("开支");
        //录入日期
        finance.setInspendDate(spend.getSpendEnteringDate());
        //支出金额
        finance.setInspendSpendmoney(spend.getSpendDebit());
        //收入金额
        finance.setInspendIncomemoney(null);
        //收支科目
        finance.setInspendCourse(spend.getSpendCourse());
        //收支类型
        finance.setInspendType(spend.getSpendType());
        //进港站点
        finance.setInspendArrivalStation(spend.getSpendArrivalStation());
        //发生站点
        finance.setInspendOccurStation(spend.getSpendSubstId());
        //收支方式
        finance.setInspendWay(spend.getSpendWay());
        //收支内容
        finance.setInspendContent(spend.getSpendContent());
        //收支操作员
        finance.setInspendOperator(spend.getSpendTypeinPerson());
        //付款对象
        finance.setInspendPayObj(spend.getSpendPayObj());
        //收支备注
        finance.setInspendRemark(spend.getSpendRemark());
        //收支个人审核
        finance.setInspendPersonCheck("否");
        //收支审核人
        finance.setInspendVerifier("");
        //收支分站审核
        finance.setInspendSubstationCheck("否");
        //收支分站审核人
        finance.setInspendSubstationVerifier("");
        //凭证号
        finance.setInspendVoucherNum(spend.getSpendVoucherNum());
        financeService.insertFinance(finance);
        return spendService.insertSpend(spend);
    }
}
